package com.matrix.utilities;

import junit.framework.TestCase;
import junit.framework.Assert;


public class ParserTest extends TestCase {
	
	private Parser p = new Parser();
	protected void setUp() throws Exception {
		super.setUp();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
	/*public void testParser()
	{
		String input = "1.222222 2e5 3";
		ParserNew p1 = new ParserNew(input);
		double[][] output = null;
		try {
			output = p1.getMatrix(input);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(output == null)
		{
			fail();
		}
	}*/
	public void testGetMatrix1()
	{
		String input = "1 2 3; 4 5 6; 7 8 9";
		double[][] output = null;
		try {
			output = p.getMatrix(input);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i=0; i<output.length; i++)
		{
			for(int j=0; j<output[0].length; j++)
			{
				System.out.print(output[i][j]+" ");
			}
			System.out.println();
		}
		double[][] expected = {{1,2,3},{4,5,6},{7,8,9}};
		assertArrayEquals(expected, output, 1e-15);
		
	}
	
	public void testGetMatrix2()
	{
		String input = "1 2 3; 4 5 6; 7 8";
		double[][] output = null;
		try {
			output = p.getMatrix(input);
			fail();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public void testGetMatrix3()
	{
		String input = "1222222 2 3; 4 5 6; 7 8 9";
		double[][] output = null;
		try {
			output = p.getMatrix(input);
			fail();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			
		}
		
	}
	public void testGetMatrix4()
	{
		String input = "1.222222 2 3; 4 5 6; 7 8 9";
		double[][] output = null;
		try {
			output = p.getMatrix(input);
			fail();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public void testGetMatrix5()
	{
		String input = "1.22222 2 3; 4 5 6; 7 8 9";
		double[][] output = null;
		try {
			output = p.getMatrix(input);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		double[][] expected = {{1.22222,2,3},{4,5,6},{7,8,9}};
		assertArrayEquals(expected, output, 1e-15);
		
	}
	public void testGetMatrix6()
	{
		String input = "a 2 3; 4 5 6; 7 8 9";
		double[][] output;
		try {
			output = p.getMatrix(input);
			fail();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	public void testGetMatrix7()
	{
		String input = "1 2 3 4 5 6 7 8 9 10 11; 1 2 3 4 5 6 7 8 9 10 11";
		double[][] output;
		try {
			output = p.getMatrix(input);
			fail();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public void testGetMatrix8()
	{
		String input = "1 2; 3 4; 5 6; 7 8; 9 10; 11 1; 2 3; 4 5; 6 7; 8 9; 10 11";
		double[][] output;
		try {
			output = p.getMatrix(input);
			fail();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public void testGetMatrix9()
	{
		String input = "1 2 3; ";
		double[][] output = null;
		try {
			output = p.getMatrix(input);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i=0; i<output.length; i++)
		{
			for(int j=0; j<output[0].length; j++)
			{
				System.out.print(output[i][j]+" ");
			}
			System.out.println();
		}
		double[][] expected = {{1,2,3},{4,5,6},{7,8,9}};
		assertArrayEquals(expected, output, 1e-15);
		
	}
	public void testGetVector()
	{
		String input = "1e-5 2 3";
		double[] output = null;
		try {
			output = p.getVector(input);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		double[] expected = {1e-5, 2, 3};
		for(int i=0; i<3; i++)
		{
			assertEquals(expected[i], output[i], 1e-15);
		}
	}
	public static void assertArrayEquals(double[][] expected, double[][] actuals, double delta)
	{
		assertArrayAreSameLength(expected,actuals);
		
		for(int i = 0; i < expected.length; ++i)
			for(int j = 0; j < expected[0].length; ++j)
			{
				assertEquals(expected[i][j], actuals[i][j], 1e-15);
			}
	}
	public static void assertArrayAreSameLength(double[][] expected, double[][] actuals)
	{
		if(expected == null) Assert.fail(" expected array was null");
		if(actuals == null) Assert.fail(" actuals array was null");
		int expectedLengthRows = expected.length;
		int expectedLengthCols = expected[0].length;
		int actualsLengthRows = expected.length;
		int actualsLengthCols = expected[0].length;
		if(expectedLengthRows != actualsLengthRows && expectedLengthCols != actualsLengthCols)
		{
			Assert.fail("Array Lengths differed, expected.row = "+expectedLengthRows+"  expected.cols = "+expectedLengthCols+" and" +
					"actuals.rows = "+actualsLengthRows+"  actuals.cols = "+actualsLengthCols);
		}
	}
}
